package com.cloudnews.model.news;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.TimeZone;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;

import com.cloudnews.EntityType;
import com.cloudnews.IndexedEntity;
import com.cloudnews.ReporterAccount;
import com.cloudnews.constant.CloudType;


@Entity
@Indexed
public class Stories implements Serializable, IndexedEntity {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue
	private Long id;
	
	@NotNull
	private String story;
	
	@Enumerated(EnumType.STRING)
	private CloudType cloudType;
	
	@Temporal(TemporalType.TIMESTAMP)
	private Date postedDate;
	
	@ManyToOne
	private ReporterAccount reporter;
	
	@NotNull
	@Field(name = "story.headline", index = Index.YES)
	private String headlines;
	
	@OneToMany(mappedBy="story")
	private List<StoryComment> comments = new ArrayList<StoryComment>();
	
	@NotNull
	private double latitude;
	
	@NotNull
	private double longitude;
	
	@OneToMany(cascade=CascadeType.ALL,mappedBy="story")
	private List<StoryLocation> locations = new ArrayList<StoryLocation>();
	
	@Temporal(TemporalType.TIMESTAMP)
	private Calendar postingTimeZone;
	
	@Transient
	private static final String[] indexedFields = { "story.headline" };

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getStory() {
		return story;
	}

	public void setStory(String story) {
		this.story = story;
	}

	public CloudType getCloudType() {
		return cloudType;
	}

	public void setCloudType(CloudType cloudType) {
		this.cloudType = cloudType;
	}

	public Date getPostedDate() {
		return postedDate;
	}

	public void setPostedDate(Date postedDate) {
		this.postedDate = postedDate;
	}

	public ReporterAccount getReporter() {
		return reporter;
	}

	public void setReporter(ReporterAccount reporter) {
		this.reporter = reporter;
	}

	public String getHeadlines() {
		return headlines;
	}

	public void setHeadlines(String headlines) {
		this.headlines = headlines;
	}

	public List<StoryComment> getComments() {
		return comments;
	}

	public void setComments(List<StoryComment> comments) {
		this.comments = comments;
	}

	public double getLatitude() {
		return latitude;
	}

	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}

	public double getLongitude() {
		return longitude;
	}

	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}

	public List<StoryLocation> getLocations() {
		return locations;
	}

	public void setLocations(List<StoryLocation> locations) {
		this.locations = locations;
	}
	
	public void addLocations(StoryLocation l){
		locations.add(l);
		l.setStory(this);
	}

	public Calendar getPostingTimeZone() {
		return postingTimeZone;
	}

	public void setPostingTimeZone(Calendar postingTimeZone) {
		this.postingTimeZone = postingTimeZone;
	}
	
	@PrePersist
	private void onSave(){
		
		postedDate = new Date();
		postingTimeZone = new GregorianCalendar(TimeZone.getTimeZone("GMT")).getInstance();
	}

	@Override
	@Transient
	public EntityType getEntityType() {
		return EntityType.STORIES;
	}

	@Override
	@Transient
	public String getEntityId() {
		return id.toString();
	}

	@Override
	@Transient
	public String getRecentViewName() {
		return getHeadlines();
	}

	@Override
	@Transient
	public String[] getIndexedFields() {
		return indexedFields;
	}

	@Override
	@Transient
	public String getSearchResultName() {
		return getHeadlines();
	}
	
}
